/**
 * 
 */
package dao.versant;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jdo.Query;

import tp.model.Client;
import tp.model.Ticket;
import tp.model.Trajet;
import dao.interfaces.ITicketDAO;

/**
 * @author Admin
 *
 */
public class TicketDAOVersant extends VersantDAO<Ticket> implements ITicketDAO {

	@Override
	public void create(Ticket ticket) {      
		pm.currentTransaction().begin();

		ticket.setClient(this.findClient(ticket.getClient()));
		ticket.setTrajet(this.findTrajet(ticket.getTrajet()));
		
        pm.makePersistent(ticket);
        pm.currentTransaction().commit();
	}
	
	private Trajet findTrajet(Trajet trajetParam) {
		// TODO Auto-generated method stub
		Query q = pm.newQuery(Trajet.class);
		q.declareParameters("String t");
		q.setFilter("nom == t");
		Trajet trajet = null;
		Collection collection;
		try {
			collection = (Collection) q.execute(trajetParam.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					trajet = (Trajet) i.next();
			}
		} finally {
			if (trajet != null)
				q.close(trajet);
		}
		return trajet;
	}

	private Client findClient(Client clientParam) {
		// TODO Auto-generated method stub
		Query q = pm.newQuery(Client.class);
		q.declareParameters("String c");
		q.setFilter("nom == c");
		Client client = null;
		Collection collection;
		try {
			collection = (Collection) q.execute(clientParam.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				if (i.hasNext())
					client = (Client) i.next();
			}
		} finally {
			if (client != null)
				q.close(client);
		}
		return client;
	}

	@Override
	public void updatePlace(Ticket ticket, String place) {
        pm.currentTransaction().begin();
        Ticket ticketPersistant = this.find(ticket);
        ticketPersistant.setPlace(place);
        pm.currentTransaction().commit();
	}

	private Ticket find(Ticket ticket) {
		Query q = pm.newQuery(Ticket.class);
		q.declareParameters("String t");
		q.setFilter("nom == t");
		Ticket ticketPersistant = null;
		try {
			ticketPersistant = (Ticket) q.execute(ticket.getNom());
		} finally {
			if (ticketPersistant != null) q.close(ticketPersistant);
		}
		return ticketPersistant;
	}
	
	@Override
	public void delete(Ticket ticket) {
        pm.currentTransaction().begin();
        pm.deletePersistent(ticket);
        pm.currentTransaction().commit();
	}
	
	@Override
	public ArrayList<Ticket> getAllActiveTickets(Client client) {
		// TODO Auto-generated method stub
		Query q = pm.newQuery(Ticket.class);
		q.declareParameters("java.util.Date d, String c");
		q.setFilter("client.nom == c && trajet.date >= d");
		ArrayList<Ticket> listTickets = new ArrayList<Ticket>();
		Collection collection;
		try {
			collection = (Collection) q.execute(new Date(), client.getNom());
			if (!collection.isEmpty()) {
				Iterator i = collection.iterator();
				while (i.hasNext()) {
					listTickets.add((Ticket) i.next());
				}
			}
		} finally {
			if (listTickets != null) q.close(listTickets);
		}
		return listTickets;
	}
	
	@Override
	public ArrayList<Ticket> findAll() {
		return null;
	}

}
